import {
  BankRollModel
} from '../../../../../models/bankroll'
const App =  getApp();

const bankrollModel = new BankRollModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    hasNext: true,
    pageIndex: 1,
    pageSize: 10,
    userCashList: [],
    loading: false,
    empty:false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false
  },

  tapToDesc: function (event) {
    const cid = event.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/manager/commision/bankroll/desc/index?cid=${cid}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.active) {
      this.setData({
        active: options.active
      })
    }
  },


  initData: function (active) {
    const self = this;
    if (active == 0) {
      bankrollModel.getCashList(this.data.pageIndex, this.data.pageSize)
      .then(res => {
        if (res.data.userCashList.length == 0) {
          self.data.empty = true
        } else {
          self.data.empty = false
        }
        this.setData({
          empty:self.data.empty,
          hasNext: res.data.hasNext,
          userCashList: res.data.userCashList
        })
      })
    } else if (active == 1) {
      bankrollModel.queryCapitalDetails2(this.data.pageIndex, this.data.pageSize).then(res => {
        if (res.data.userCashList.length == 0) {
          self.data.empty = true
        } else {
          self.data.empty = false
        }
        this.setData({
          empty:self.data.empty,
          hasNext: res.data.hasNext,
          userCashList: res.data.userCashList
        })
      }).catch()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData(this.data.active)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageIndex:1, 
      pageSize: 10
    })
    this.initData()
    wx.stopPullDownRefresh()
  },


  //加载更多
  loadMore(isPull) {
    if (this.data.loading) {
      return
    }
    if (this.data.hasNext) {
      const params = {
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize,
      }
      if (isPull) {
        this.setData({
          loading: true
        })
      }
      const that = this
      if (that.data.active == 0) {
        bankrollModel.getCashList(params).then(res => {
          const tempArray = that.data.userCashList.concat(res.data.userCashList)
          that.setData({
            userCashList: tempArray,
            hasNext: res.data.hasNext,
            loading: false
          })
        })
      } else if (this.data.active == 1) {
        bankrollModel.queryCapitalDetails2(params).then(res => {
          const tempArray = that.data.userCashList.concat(res.data.userCashList)
          that.setData({
            userCashList: tempArray,
            hasNext: res.data.hasNext,
            loading: false
          })
        })
      }
      
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.hasNext) {
      return
    }
    this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },

})